package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.common.annotation.Authorization;
import com.yxh.pojo.Group;
import com.yxh.pojo.Project;
import com.yxh.service.ProjectService;
import com.yxh.util.APIErrorCode;
import com.yxh.util.Response;
import com.yxh.util.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import static com.yxh.util.APIErrorCode.DELETE_ERROR;
import static com.yxh.util.APIErrorCode.UPDATE_ERROR;
import static com.yxh.util.APIErrorCode.SUCCESS;

/**
 * Created by asus on 2018/6/1.
 */

@Scope("prototype")
@RestController
@RequestMapping(value = "/project")
public class ProjectController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);
    private Response response;
    @Autowired
    private ProjectService projectService;

    /**
     *新增
     *
     * @param project
     * @return
     */
    @PostMapping
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response saveGroup(@RequestBody Project project) {

        //System.out.println(SnowflakeIdWorker.creatId());
        //  project.setId(SnowflakeIdWorker.creatId());
        project.setProjectId(SnowflakeIdWorker.creatId());
        response = Response.returnResponse(APIErrorCode.SUCCESS);
        response.setData(projectService.save(project));
        return response;
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response removeDept(@PathVariable("id") String id) {
       String []ids=id.split(",");
        int i=0;
        if(ids.length==0){
            i=projectService.delete(id);
        }
        else{
            for (String id1 : ids) {
                i = projectService.delete(id1);
            }
        }
        if(i==0)
            return Response.returnResponse(DELETE_ERROR);
        else
            return Response.returnResponse(SUCCESS);
    }


    @GetMapping
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getInformation(Project deptJson, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = projectService.getInformation(deptJson, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);
        return response;
    }


    @GetMapping("/app")
    //@Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response getInformationApp(String value, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = projectService.getInformationApp(value, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);
        return response;
    }
    /**
     * 更新
     *
     * @param
     * @return
     */
    @PutMapping
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response updateDept(@RequestBody Project deptJson) {
        int i=projectService.update(deptJson);
        if(i==0)
            return Response.returnResponse(UPDATE_ERROR);
        else
            return Response.returnResponse(SUCCESS);
    }

}
